define([
        'jquery',
        'Models/User'



        ], function ($, User){

	var AjaxController = function(maincontroll) {
		
		this.mainController = maincontroll;

	};
	
	




	AjaxController.prototype = {

			contact : function(method, url, params) {
				console.log(this);
				$.ajax({

					url : url
					, type : method
					, data : params
					, context : this
					, dataType : 'text' 
						, success : function(data) {

							console.log("session ID : " +data);

							this.result(data, method);

						}
				});
			},
			getData : function(method, url, params, func, info) {
				
				$.ajax({

					url : url
					, type : method
					, data : params
					, context : this
					, dataType : 'json' 
						, success : function(data) {
							
							var res = data;

							
								if(info == "user") {
									
									console.dir(res);
							
									var id = res.id;
									var password = res.password;
									var name = res.name;
									var age = res.age;
									var user = new User(id, password, name, age);
									this.mainController.router("update",id,user);
									
								}else if(info == "folder" || info == "memo") {
									if(func !== null  && func !== undefined) {
										console.dir(res);
										func(res);
									}
									
								}else if(info == "folderNum") {
									alert('폴더가 생성되었습니다!');
									func(res);
									
								}else if(info == "memoContent") {
									
									alert('메모가 저장되었습니다!');
					
									//func(res);
									
								}
									

								
							

						}
				});
			},

			result : function(res, method) {			

				var joinSuccess = 1,
				duplicatedId = -2, 
				unknownError = -1, 
				PassError = 3, 
				idError = 10, 
				removed = 13, 
				FolderUpdateSuccess = -6; 
				MemoUpdateSuccess = -7;
				DeleteFolderSuccess = -8;
				DeleteMemoSuccess = -9;
				
				if(method == "POST") {
					if(res ==  joinSuccess) {
						alert('회원가입 성공했습니다!');
						this.mainController.router("login");             
						res = null;
					}else if(res == duplicatedId) {
						alert('아이디 중복입니다!');
						res = null;
					}else if(res == unknownError) {
						alert('알려지지 않은 에러입니다!');
						res = null;
					}else if(res == PassError) {
						alert('비밀번호 일치하지 않습니다!');
						res = null;
					}else if(res == idError) {
						alert('아이디가 존재하지 않습니다!');
					}else if(res == removed) {
						console.log('삭제');
						alert('삭제되었습니다!');
						this.mainController.router("login");                  
					}else if(res == FolderUpdateSuccess) {
						alert('폴더가 수정되었습니다!');

					}else if(res == DeleteFolderSuccess) {
						alert('폴더가 삭제되었습니다!');

					}else if(res == MemoUpdateSuccess) {
						alert('메모가 저장되었습니다!');

					}else if(res == DeleteMemoSuccess) {
						alert('메모가 삭제되었습니다!');

					}else if(typeof res === 'string') {  

						this.mainController.router("memo",res);

					}

				}else if (method == "GET") {

					//아이디가 있을때
					if(res != "") {

						this.mainController.router("memo",res);
					}
					//아이디가 없을때
					else {
						console.log("ajax ="+this);
						this.mainController.router("login"); 


					}
				}							
			}

	};
	return AjaxController;
});

